package com.fitns.user.controller.baseConfig;

import com.fitns.user.conf.Resp;
import com.fitns.user.dto.baseConfig.SysDictDTO;
import com.fitns.user.dto.baseConfig.SysDictVO;
import com.fitns.user.service.baseConfig.SysDictService;
import com.fitns.user.utils.ResultData;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *Created by ye on 2018/8/1
 *
 */

@RestController
@CrossOrigin //解决前后端对接跨域问题
@RequestMapping("/sysDict")
@Api(description = "系统字典维护")
public class SysDictController {

    @Autowired
    private SysDictService sysDictService;

    @PostMapping("/insert")
    @ApiOperation("新增")
    public ResultData insert(@RequestBody SysDictDTO sysDictDTO){
        SysDictVO sysDictVO = sysDictService.insert(sysDictDTO);
        return new ResultData(200,"SUCESS",sysDictVO,"");
    }

    @PutMapping("/modify")
    @ApiOperation("修改")
    public ResultData modify(@RequestBody SysDictDTO sysDictDTO){
        SysDictVO sysDictVO = sysDictService.modify(sysDictDTO);
        if(StringUtils.isEmpty(sysDictVO)){
            return new ResultData( 300,"DEFAULT",sysDictVO,"");
        }
        return new ResultData(200,"SUCESS",sysDictVO,"");
   }

    @GetMapping("/select")
    @ApiOperation("查询列表")
    public Object select( SysDictDTO sysDictDTO){
        PageInfo<SysDictVO> sysDictVOList= sysDictService.selectList(sysDictDTO);
        sysDictVOList.setPageSize(sysDictDTO.getPageSize());
        return Resp.page(sysDictVOList);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public ResultData delete(@RequestParam("id") Long id){
        sysDictService.delete(id);
        return new ResultData(200,"SUCESS",null,"删除成功");
    }

    @GetMapping("/selectById")
    @ApiOperation("通过id查询数据")
    public Object selectById(@RequestParam("id") Long id){
        return sysDictService.selectById(id);
    }

    @GetMapping("/searchDict")
    @ApiOperation("确认记录下边的数据信息")
    public Object searchDict(@RequestParam("id") Long id) {
        SysDictDTO sysDictDTO = new SysDictDTO();
        sysDictDTO.setId(id);
        List<SysDictVO> sysDictVOList = sysDictService.searchDict(sysDictDTO);
        return sysDictVOList;
    }

    @GetMapping("/getListByCode")
    @ApiOperation("获取字典下的值")
    public Object getListByCode(@RequestParam("dictCode") String dictCode, @RequestParam("type") Long type) {
        SysDictDTO sysDictDTO = new SysDictDTO();
        sysDictDTO.setDictCode(dictCode);
        sysDictDTO.setType(type);
        List<SysDictVO> sysDictVOList = sysDictService.searchDict(sysDictDTO);
        return sysDictVOList;
    }

}
